package service

import(
	"go_code/customerManager/model"
	
)
// 该CustomerService 完成对Customer的crud
type CustomerService struct{
	customers []model.Customer
	// 声明一个字段，表示当前切片含有多少个客户
	// 该字段后面，还可以作为新客户的id+1
	customerNum int

}

// 编写一个方法 可以返回 *CustomerService
func NewCustomerService ()*CustomerService{
	customerService := &CustomerService{}
	customerService.customerNum = 1
	customer := model.NewCustomer(1,"张三","男",20,"112","zs@qq.com")
	customerService.customers = append(customerService.customers,customer)
	return customerService
}

// 返回客户切片
func(this *CustomerService) List() []model.Customer{
	return this.customers
}

// 添加客户到customers切片
// 如果是不带星号 那就是值拷贝，每次展示的都是最新的，原先就丢掉了，因为指向的地址不同
// 因此，传的是一个地址，始终只有一个实例
func(this *CustomerService) Add(customer model.Customer) bool{
	this.customerNum++
	customer.Id = this.customerNum
	this.customers = append(this.customers,customer)
	return true
}
// 根据id查找客户在切片中对应的下标，如果没有该客户，返回-1
func(this *CustomerService)FindById(id int)int{
	index :=-1
	// 遍历
	for i:=0;i<len(this.customers);i++{
		if this.customers[i].Id == id {
			index = i
			break
		}
	}

	return index
}
func(this *CustomerService)Delete(id int) bool{
	index :=this.FindById(id)
	if index == -1 {
		return false
	}
	// 如何从切片中删除一个元素
	this.customers = append(this.customers[:index],this.customers[index+1:]...)
	return true
}

func(this *CustomerService)FindUpdate(id int)(index int, isFind bool){
	 index = this.FindById(id)

	if index == -1 {
		return  -1,false
	}
	this.customers[index].GetInfo()
	return index, true
}

func (this *CustomerService)Update(index int,customer model.Customer) bool {
	id :=this.customers[index].Id 
	customer.Id = id
	this.customers[index] = customer
	
	return true
}


